package com.myapp02_1;

import java.text.DecimalFormat;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ActMain extends Activity {
	
	
	double n1=0,all=0;	
	String s = "";
	DecimalFormat p = new DecimalFormat("0.0000");; //小數點控制

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.actmain);
		InitialComponet();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.act_main, menu);
		return true;
	}
	
	public void total()
	{	
		
		n1 = Double.parseDouble(result.getText().toString());
		
		System.out.println
			(" 計算前 all="+all+" n1="+n1+" 符號:"+s);
		if(s==""){
			//第一次初始all值
			all=n1;
		}
		
		else if (s=="+"){	
			this.goplus();
		}
		else if (s=="-"){	
			this.gosub ();
		}
		else if (s=="*"){	
			this.gomult();
		}
		else if (s=="/"){	
			this.godiv ();
		}
		
		System.out.println("計算後 all="+all);
	}
	private void goplus(){	 
		all += n1; 
	}	// +
	private void gosub() {	 
		all -= n1; 
	}	// -
	private void gomult(){	 
		all *= n1; 
	}	// *
	private void godiv() { // /
		all = (int)all/(int)n1; 
	}	
	
	private void InitialComponet() {

		one		= (Button) super.findViewById(R.id.one);
		two		= (Button) super.findViewById(R.id.two);
		three	= (Button) super.findViewById(R.id.three);
		four	= (Button) super.findViewById(R.id.four);
		five 	= (Button) super.findViewById(R.id.five);
		six 	= (Button) super.findViewById(R.id.six);
		seven 	= (Button) super.findViewById(R.id.seven);
		eight 	= (Button) super.findViewById(R.id.eight);
		nine 	= (Button) super.findViewById(R.id.nine);
		zero 	= (Button) super.findViewById(R.id.zero);
		
		plus 	= (Button) super.findViewById(R.id.plus);
		sub 	= (Button) super.findViewById(R.id.sub);
		mult 	= (Button) super.findViewById(R.id.mult);
		div 	= (Button) super.findViewById(R.id.div);
		equ 	= (Button) super.findViewById(R.id.equal);
		result  = (TextView) super.findViewById(R.id.result);
		
		one.setOnClickListener(one_btn);
		two.setOnClickListener(two_btn);
		three.setOnClickListener(three_btn);
		four.setOnClickListener(four_btn);
		five.setOnClickListener(five_btn);
		six.setOnClickListener(six_btn);
		seven.setOnClickListener(seven_btn);
		eight.setOnClickListener(eight_btn);
		nine.setOnClickListener(nine_btn);
		zero.setOnClickListener(zero_btn);
		
		plus.setOnClickListener(plus_btn);
		sub.setOnClickListener(sub_btn);
		mult.setOnClickListener(mult_btn);
		div.setOnClickListener(div_btn);
		
		equ.setOnClickListener(result_btn);
		
	}
	
	OnClickListener one_btn = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			if(result.getText()==""){
				result.setText("1");
			}else{				
				temp = result.getText().toString()+"1";
				result.setText(temp);
			}
		}
	};
	OnClickListener two_btn = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			if(result.getText()==""){
				result.setText("2");
			}else{				
				temp = result.getText().toString()+"2";
				result.setText(temp);
			}
		}
	};
	OnClickListener three_btn = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			if(result.getText()==""){
				result.setText("3");
			}else{				
				temp = result.getText().toString()+"3";
				result.setText(temp);
			}
		}
	};
	OnClickListener four_btn = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			if(result.getText()==""){
				result.setText("4");
			}else{				
				temp = result.getText().toString()+"4";
				result.setText(temp);
			}
		}
	};
	OnClickListener five_btn = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			if(result.getText()==""){
				result.setText("5");
			}else{				
				temp = result.getText().toString()+"5";
				result.setText(temp);
			}
		}
	};
	OnClickListener six_btn = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			if(result.getText()==""){
				result.setText("6");
			}else{				
				temp = result.getText().toString()+"6";
				result.setText(temp);
			}
		}
	};
	OnClickListener seven_btn = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			if(result.getText()==""){
				result.setText("7");
			}else{				
				temp = result.getText().toString()+"7";
				result.setText(temp);
			}
		}
	};
	OnClickListener eight_btn = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			if(result.getText()==""){
				result.setText("8");
			}else{				
				temp = result.getText().toString()+"8";
				result.setText(temp);
			}
		}
	};
	OnClickListener nine_btn = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			if(result.getText()==""){
				result.setText("9");
			}else{				
				temp = result.getText().toString()+"9";
				result.setText(temp);
			}
		}
	};
	OnClickListener zero_btn = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			if(result.getText()==""){
				result.setText("0");
			}else{				
				temp = result.getText().toString()+"0";
				result.setText(temp);
			}
		}
	};
	OnClickListener result_btn = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			total();	 //最後結果
			System.out.println("測試:"+p.format(all));
        	result.setText(p.format(all));
            s = ""; all = 0; n1= 0;
		}
	};
	
	
	OnClickListener plus_btn = new OnClickListener() {
		@Override
		public void onClick(View arg0) {

			total();
			s = "+";
			result.setText("");
		}
	};
	OnClickListener sub_btn = new OnClickListener() {
		@Override
		public void onClick(View arg0) {

			total();
			s = "-";
			result.setText("");
		}
	};
	OnClickListener mult_btn = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			
			total();
			s = "*";
			result.setText("");
		}
	};
	OnClickListener div_btn = new OnClickListener() {
		@Override
		public void onClick(View arg0) {

			total();
			s = "/";
			result.setText("");
		}
	};
	
	
	String op =null;
	String temp=null;	
	Button one = null;
	Button two = null;
	Button three = null;
	Button four = null;
	Button five = null;
	Button six = null;
	Button seven = null;
	Button eight = null;
	Button nine = null;
	Button zero = null;
	Button plus = null;
	Button sub = null;
	Button mult = null;
	Button div = null;
	Button equ = null;
	TextView result = null; 
}
